#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
//#extension GL_EXT_shader_16bit_storage : require


#include "../../../../core/shader_std.h"
#include "../../../../core/shader_built_in_functions.h"
#include "../../../../core/shader_mesh.h"

layout(push_constant, scalar) uniform DRAWPROP_push_constants {
	S_ConstPush_Mesh_GraduationLine const_push;
};


layout(std140, set = 0, scalar, column_major, binding = DEF_BINDING_PROJECT) uniform Buf0 {
	mat4 mat_ProjecView[4];
};

layout(std430, set = 1, scalar, column_major, binding = DEF_binding_L1_3D_Tranform) readonly buffer Buf1 {
	mat4 mat_Model[];
};


//layout(location = 0) in vec3 inPos;
//layout(location = 1) in vec3 inNormal;
//layout(location = 2) in vec3 inUV_Color;


layout(location = 0) out VS_GS_VERTEX {
	vec4 vVertB;
	vec4 vVertE;
	vec4 vColor;
	vec4 vPointB;
	vec4 vPointE;
	vec4 vEye;
	float vDis;
}vertex_out;

out gl_PerVertex {
	vec4 gl_Position;
	float gl_PointSize;
};


//vec3 pos[4] = {vec3(10.0, -10.0, 10), vec3(-100.0, 10.0,-2), vec3(10.0, 100.0,0), vec3(100.0, 100.0,0)};

void main(void){
	mat4 bonesMat_som = mat_Model[const_push.m_Offset_Tranform];
	//mat4 mat_proj = mat_ProjecView[DEF_Camera_Matrix_PV];

	//mat_proj = mat4(0);
	//float f = 100;
	//float n = 0.1;
	//float tanHalfFovy = tan(radians(45) * 0.5f);
	//mat_proj[0][0] = 1.0f / ((1920.0/1080.0) * tanHalfFovy);
	//mat_proj[1][1] = 1.0f / tanHalfFovy;
	//mat_proj[2][2] = f / (f - n);
	//mat_proj[2][3] = 1.0f;
	//mat_proj[3][2] = -(f * n) / (f - n);
	uint lineNum = const_push.m_Grid_HNum;
	

	
	//vertex_out.vVertB = mat_ProjecView[DEF_Camera_Matrix_Porj] * mat_ProjecView[DEF_Camera_Matrix_View] * vec4(pos[gl_VertexIndex], 1);
	//return;

	float gap = -const_push.m_CellSize * ((lineNum+1)>>1);
	vec4 vert1 = vec4(0,0,0,1);
	vec4 vert2 = vec4(0,0,0,1);
	
	
	//vertex_out.vDis = const_push.m_CameraDis * 3;
	vertex_out.vDis = max(3, max(abs(vertex_out.vEye.y) * 3, const_push.m_CameraDis * 3));

	bool sw = false;
	if ( bool(gl_VertexIndex&1) ) {
		gap += (const_push.m_CellSize) * float(gl_VertexIndex+1);
		
		vert1.x = -vertex_out.vDis;
		vert2.x = vertex_out.vDis;

		vert1.z = gap; 
		vert2.z = gap;
	} 
	else{
		gap += (const_push.m_CellSize) * float(gl_VertexIndex+1) + const_push.m_CellSize;

		vert1.x = gap;
		vert2.x = gap;

		vert1.z = -vertex_out.vDis;
		vert2.z = vertex_out.vDis;
	}

	if(const_push.m_Grid_HNum <= 1){
		vertex_out.vColor.rgb = abs(normalize(vert2.xyz - vert1.xyz) + 0.5) * 0.6;
		vertex_out.vColor.a = 1.0;
	}
	else {
		vertex_out.vColor = f_glsl_buildColor(const_push.m_Ob_Color);
	}


	vertex_out.vEye = (inverse(mat_ProjecView[DEF_Camera_Matrix_View]) * vec4(0,0,0,1));


	vertex_out.vVertB = (bonesMat_som * vert1);
	vertex_out.vVertE = (bonesMat_som * vert2);

	vertex_out.vPointB = vertex_out.vVertB;
	vertex_out.vPointE = vertex_out.vVertE;

	
	
	vertex_out.vVertB = mat_ProjecView[DEF_Camera_Matrix_PV] * vertex_out.vVertB;
	vertex_out.vVertE = mat_ProjecView[DEF_Camera_Matrix_PV] * vertex_out.vVertE;
	
	vertex_out.vVertB.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy / vertex_out.vVertB.w;
	vertex_out.vVertE.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy / vertex_out.vVertB.w;
	
	vec4 ndcposB = vertex_out.vVertB / vertex_out.vVertB.w;
	vec4 ndcposE = vertex_out.vVertE / vertex_out.vVertE.w;
	ndcposB.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy;
	ndcposE.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy;
	ndcposB.z += 0.0001;
	ndcposE.z += 0.0001;
	vertex_out.vVertB = ndcposB * vertex_out.vVertB.w;
	vertex_out.vVertE = ndcposE * vertex_out.vVertE.w;

	gl_PointSize = 100;
}


 

